<template>
  <table border="1">
    <tr>
      <th>书名</th>
      <th>价格</th>
      <th>库存</th>
      <th>操作</th>
    </tr>
    <tr v-for="a in all">
      <td>{{a.title}}</td>
      <td>{{a.price}}</td>
      <td>{{a.num}}</td>
      <td><button @click="gou(a.id)">购买</button></td>
    </tr>
  </table>

</template>

<script>
  import axios from 'axios'
    export default {
        name: "Books",
      data(){
          return{
            all:[]
          }
      },
      mounted() {
          axios({
            url:'http://127.0.0.1:8000/books/',
            method:'post',
          }).then(res=>{
            console.log(res);
            this.all = res.data.data
          })
      },
      methods:{
          gou(bid){
            var uid = localStorage.getItem('uid');
            if (uid==null){
              this.$router.push({'name':'Login'})
            }else {
              let from_data = new FormData();
              from_data.append('bid',bid);
              from_data.append('uid',localStorage.getItem('uid'))
              axios({
                url: 'http://127.0.0.1:8000/addcart/',
                method: 'post',
                data:from_data

            }).then(res=>{
              alert(res.data.msg)
              })
            }

          }
      }
    }
</script>

<style scoped>

</style>
